package com.zhk.ScriptManagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhk.ScriptManagement.entity.ScriptType;
import com.zhk.ScriptManagement.entity.vo.ScriptInfoVo;
import com.zhk.ScriptManagement.entity.ScriptRelationType;
import com.zhk.ScriptManagement.entity.vo.ScriptVo;
import com.zhk.ScriptManagement.mapper.ScriptMapper;
import com.zhk.ScriptManagement.service.ScriptService;
import com.zhk.ScriptManagement.util.CodeMsg;
import com.zhk.ScriptManagement.util.Result;
import com.zhk.ScriptManagement.util.TokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: zhouhongkun
 * @Description: 剧本service实现类
 * @Date: 2021/4/5 11:56
 */
@Service
public class ScriptServiceImpl implements ScriptService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ScriptMapper scriptMapper;




    @Override
    public Result getFuzzyScripts(String scriptName, Integer scriptTypeId, Integer scriptNum,String size, String num,String token) {
        try{
            Integer userId = TokenUtil.getField(token, "userId", Integer.class);
            Integer pageSize = null;
            Integer pageNum = null;
            if(Optional.ofNullable(size).isPresent() && Optional.ofNullable(num).isPresent()){
                pageNum = Integer.parseInt(num);
                pageSize = Integer.parseInt(size);
            }
            PageHelper.startPage(pageNum,pageSize);
            List<ScriptVo>  scriptVoList = scriptMapper.getFuzzyScripts(scriptName,scriptTypeId,scriptNum,userId);
            PageInfo<ScriptVo> scriptVoPageInfo = new PageInfo<>(scriptVoList);
            return Result.success(scriptVoPageInfo);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error(CodeMsg.SERVER_EXCEPTION,"查询失败");
        }
    }

    @Override
    public Result getScriptByScriptId(Integer scriptId) {
        try{
            ScriptVo scriptVo = scriptMapper.getScriptByScriptId(scriptId);
            return Result.success(scriptVo);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error(CodeMsg.SERVER_EXCEPTION,"查询失败");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public Result addScript(ScriptInfoVo scriptInfoVo) {
        try{
            scriptMapper.addScript(scriptInfoVo);
            scriptMapper.addCurrentScript(scriptInfoVo);
             scriptInfoVo.getScriptTypeIds().forEach(scriptTypeId -> {
                ScriptRelationType scriptType = new ScriptRelationType();
                scriptType.setScriptId(scriptInfoVo.getScriptId());
                scriptType.setScriptTypeId(scriptTypeId);
                scriptMapper.addScriptTypes(scriptType);
            });
            return Result.success("添加成功");
        }catch (Exception e){
            log.error(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus();
            return Result.error(CodeMsg.SERVER_EXCEPTION,"添加失败");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public Result updateScript(ScriptInfoVo scriptInfoVo) {
        try{
            scriptMapper.updateScript(scriptInfoVo);
            scriptMapper.deleteScriptType(scriptInfoVo.getScriptId());
            if(scriptInfoVo.getState() == 0){
                scriptMapper.deleteCurrentScript(scriptInfoVo.getScriptId());
            }else{
                scriptInfoVo.getScriptTypeIds().forEach(scriptTypeId -> {
                    ScriptRelationType scriptType = new ScriptRelationType();
                    scriptType.setScriptId(scriptInfoVo.getScriptId());
                    scriptType.setScriptTypeId(scriptTypeId);
                    scriptMapper.addScriptTypes(scriptType);
                });
            }
            return Result.success("更新成功");
        }catch (Exception e){
            log.error(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus();
            return Result.error(CodeMsg.SERVER_EXCEPTION,"更新失败");
        }
    }

    @Override
    public Result addScriptType(ScriptType scriptType) {
        try{
            scriptMapper.addScriptType(scriptType);
            return Result.success("添加成功");
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error(CodeMsg.SERVER_EXCEPTION,"添加失败");
        }
    }

    @Override
    public Result updateScriptType(ScriptType scriptType) {
        try{
            scriptMapper.updateScriptType(scriptType);
            return Result.success("修改成功");
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error(CodeMsg.SERVER_EXCEPTION,"修改失败");
        }
    }

    @Override
    public Result getScriptTypes() {
        try{
            List<ScriptType> scriptTypeList = scriptMapper.getScriptTypes();
            return Result.success(scriptTypeList);
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.error(CodeMsg.SERVER_EXCEPTION,"查询失败");
        }
    }
}
